package com.bonc.dataplatform.bdi.define;


import com.google.gson.Gson;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.*;

/**
 * @author yangjy
 * @version 1.0
 * @description: 将邮件发送内容转为HTML格式
 * @date 2022/6/8 15:14
 */
public class MailContent2Html {
    private static final Logger log = LoggerFactory.getLogger(MailContent2Html.class);

    public Map<String, Object> content2Html(Map<String, Object> map) throws Exception {
        Map<String, Object> result = new HashMap<>(2);
        String emailHeader = map.get("emailHeader").toString().trim();
        String emailContent = map.get("emailContent").toString().trim();
        String emailTail = map.get("emailTail").toString().trim();
        if (null == emailHeader || emailHeader.equals("")) {
            throw new Exception("邮件头为空：" + emailHeader);
        }
        if (emailHeader.startsWith("$")) {
            throw new Exception("邮件头格式不正确：" + emailHeader);
        }

        if (null == emailContent || emailContent.equals("")) {
            throw new Exception("邮件内容为空：" + emailContent);
        }
        if (emailContent.startsWith("$")) {
            throw new Exception("邮件内容格式不正确：" + emailContent);
        }
        try {
            String spliceHtml = spliceHtml(emailHeader, emailTail, emailContent);
            result.put("html", spliceHtml);
        } catch (Exception e) {
            e.printStackTrace();
            log.error("html转换失败：" + e.getMessage());
            throw new Exception(e.getMessage());
        }
        return result;
    }


    public static String spliceHtml(String emailHeader, String emailTail, String emailContentList) {
        Gson gson = new Gson();
        List<Map<String, String>> contentArray = gson.fromJson(emailContentList, List.class);
        StringBuffer emailHtml = new StringBuffer();
        String[] emailHeaderArr = emailHeader.split("\\r?\\n");
        for (int i = 0; i < emailHeaderArr.length; i++) {
            if (i == 0) {
                emailHtml.append(emailHeaderArr[i].trim());
            } else if (i == 1) {
                emailHtml.append("</br>");
                emailHtml.append("<p style=\"text-indent: 2em;margin:0px; padding:0px;\">" + emailHeaderArr[i].trim() + "</p>");
            } else {
                emailHtml.append("<p style=\"margin:0px; padding:0px;\">" + emailHeaderArr[i].trim() + "</p>");
            }
        }
        emailHtml.append("</br>");
        for (Map<String, String> contentObj : contentArray) {
            String desc = contentObj.get("desc");
            String content = contentObj.get("content");
            String sort = contentObj.get("sort");
            List<String> sortList = Arrays.asList(sort.split("\\|"));
            String[] descArr = desc.split("\\r?\\n");
            for (int i = 0; i < descArr.length; i++) {
                emailHtml.append(descArr[i].trim());
                emailHtml.append("</br>");
            }
            emailHtml.append("<table border=\"1\" bordercolor=\"#979696\" style=\"border-collapse:collapse;font-size: 14px\">");
            emailHtml.append("<thead>");
            emailHtml.append("<tr>");
            for (String title : sortList) {
                String newTitle = title.replaceAll("#", "<br/>");
                emailHtml.append("<th style=\"word-break:keep-all;white-space:nowrap;background-color: #B00004;color: #fff;padding: 8px;text-align: center; \">" + newTitle + "</th>");
            }
            emailHtml.append("</tr>");
            emailHtml.append("</thead>");
            //内容
            if (!"[]".equals(content)) {
                emailHtml.append("<tbody>");
                List<Map<String, String>> dealContentMapList = dealContent(content);
                for (Map<String, String> stringMap : dealContentMapList) {
                    emailHtml.append("<tr>");
                    for (String key : sortList) {
                        String val = stringMap.get(key.replaceAll("#", "").trim());
                        //判断以负号开头 标红
                        if (val.startsWith("-")) {
                            emailHtml.append("<td style=\"padding: 8px;text-align: right;color:red \">" + val + "</td>");
                        } else {
                            emailHtml.append("<td style=\"padding: 8px;text-align: right; \">" + val + "</td>");
                        }
                    }
                    emailHtml.append("</tr>");
                }
                emailHtml.append("</tbody>");
            }
            emailHtml.append("</table>");
            emailHtml.append("</br>");
        }
        emailHtml.append("<p style = \"color: #9F9F9F\">--------------------------------------------------------------------------------</p>");
        if (null != emailTail && !"".equals(emailTail)) {
            emailHtml.append(emailTail);
            emailHtml.append("<br/>");
        }
        emailHtml.append("如果您错误接收了该邮件，请通过电子邮件立即通知我们。\n" +
                "请回复邮件到 hqs-spmc@chinaunicom.cn，即可以退订此邮件。\n" +
                "我们将立即将您的信息从我们的发送目录中删除。\n" +
                "If you have received this email in error please notify us immediately by e-mail.\n" +
                "Please reply to hqs-spmc@chinaunicom.cn ,you can unsubscribe from this mail.\n" +
                "We will immediately remove your information from send catalogue of our.");
        return emailHtml.toString();
    }

    public static List<Map<String, String>> dealContent(String originContent) {
        if (null != originContent && !originContent.equals("")) {
            List<Map<String, String>> mapListResult = new ArrayList<>();
            //originContent = "[{办理人2=sdsc-miaoxl7, 服务号码2=053000035531, 账期2=20220428, 产品编码2=52747705}, {办理人2=sd-zhuhc, 服务号码2=053000037427, 账期2=20220428, 产品编码2=51003293}, {办理人2=sd-konggy, 服务号码2=053000057987, 账期2=20220428, 产品编码2=52747700}, {办理人2=sdsc-duanqq, 服务号码2=053000279520, 账期2=20220428, 产品编码2=51003293}]";
            //掐头去尾：
            String content = originContent.replace("[{", "").replace("}]", "");
            //根据}, {分割
            //commaSignContentList：办理人2=sdsc-miaoxl7, 服务号码2=053000035531, 账期2=20220428, 产品编码2=52747705
            List<String> commaSignContentList = Arrays.asList(content.split("}, \\{"));
            for (String commaSignContent : commaSignContentList) {
                //根据,分割
                List<String> equalSignContentList = Arrays.asList(commaSignContent.split(","));
                Map<String, String> hashMap = new HashMap<>(16);
                for (String equalSignContent : equalSignContentList) {
                    //根据=分割
                    String[] keyValArr = equalSignContent.split("=");
                    if (keyValArr.length == 2) {
                        hashMap.put(keyValArr[0].trim(), keyValArr[1].trim());
                    } else {
                        hashMap.put(keyValArr[0].trim(), "");
                    }
                }
                mapListResult.add(hashMap);
            }
            return mapListResult;
        }
        log.error("originContent邮件内容为空！");
        return null;
    }

//    public static void main(String[] args) throws Exception {
//        String emailHeader = "您好，\n" +
//                "    查询。\n" +
//                "    查询。\n " +
//                "查询";
//        String emailTail = "尾";
//        String emailContentList = "[{\n" +
//                "\t\"key\": \"0\",\n" +
//                "\t\"content\": \"[]\",\n" +
//                "\t\"desc\": \"数据1\n" +
//                "数据1\n" +
//                "数据1\",\n" +
//                "\t\"sort\": \"地市#名称|在网用户数|日计费收入|月累计费收入|日新发展用户数|月累新发展用户数\"\n" +
//                "}, {\n" +
//                "\t\"key\": \"1\",\n" +
//                "\t\"desc\": \"数据2\n" +
//                "数据2\n" +
//                "数据2\",\n" +
//                "\t\"content\": \"[{月累新发展用户数=-129188, 在网用户数=, 日新发展用户数=17858, 地市名称=全省, 月累计费收入=1338338347.07, 日计费收入=7002285.58, 排序=00}, {月累新发展用户数=19014, 在网用户数=4447595, 日新发展用户数=2865, 地市名称=济南, 月累计费收入=189816592.88, 日计费收入=1110822.94, 排序=01}, {月累新发展用户数=16140, 在网用户数=4719064, 日新发展用户数=2479, 地市名称=青岛, 月累计费收入=208100946.59, 日计费收入=1059240.86, 排序=02}, {月累新发展用户数=5283, 在网用户数=1659434, 日新发展用户数=750, 地市名称=淄博, 月累计费收入=67424448.95, 日计费收入=372511.90, 排序=03}, {月累新发展用户数=3562, 在网用户数=1142998, 日新发展用户数=491, 地市名称=枣庄, 月累计费收入=40901804.60, 日计费收入=174354.76, 排序=04}, {月累新发展用户数=4108, 在网用户数=939190, 日新发展用户数=471, 地市名称=东营, 月累计费收入=42412857.74, 日计费收入=127362.64, 排序=05}, {月累新发展用户数=7342, 在网用户数=3028003, 日新发展用户数=1041, 地市名称=烟台, 月累计费收入=114625432.16, 日计费收入=484187.41, 排序=06}, {月累新发展用户数=11350, 在网用户数=2684760, 日新发展用户数=1256, 地市名称=潍坊, 月累计费收入=110421972.26, 日计费收入=425694.92, 排序=07}, {月累新发展用户数=8489, 在网用户数=2479735, 日新发展用户数=1074, 地市名称=济宁, 月累计费收入=89311243.23, 日计费收入=507328.40, 排序=08}, {月累新发展用户数=6924, 在网用户数=1596918, 日新发展用户数=859, 地市名称=泰安, 月累计费收入=52880958.50, 日计费收入=277985.36, 排序=09}, {月累新发展用户数=3835, 在网用户数=1409916, 日新发展用户数=564, 地市名称=威海, 月累计费收入=54567389.00, 日计费收入=234629.56, 排序=10}, {月累新发展用户数=2744, 在网用户数=679743, 日新发展用户数=408, 地市名称=日照, 月累计费收入=23989561.35, 日计费收入=152852.21, 排序=11}, {月累新发展用户数=1768, 在网用户数=429665, 日新发展用户数=225, 地市名称=莱芜, 月累计费收入=14762638.02, 日计费收入=73092.18, 排序=12}, {月累新发展用户数=11316, 在网用户数=2823682, 日新发展用户数=1614, 地市名称=临沂, 月累计费收入=115174822.95, 日计费收入=600165.36, 排序=13}, {月累新发展用户数=5687, 在网用户数=1541748, 日新发展用户数=810, 地市名称=德州, 月累计费收入=52628448.76, 日计费收入=298979.35, 排序=14}, {月累新发展用户数=6413, 在网用户数=1400742, 日新发展用户数=838, 地市名称=聊城, 月累计费收入=49009406.75, 日计费收入=416599.21, 排序=15}, {月累新发展用户数=3913, 在网用户数=932776, 日新发展用户数=605, 地市名称=滨州, 月累计费收入=38802350.99, 日计费收入=149496.92, 排序=16}, {月累新发展用户数=11300, 在网用户数=2070414, 日新发展用户数=1508, 地市名称=菏泽, 月累计费收入=73507370.34, 日计费收入=536981.60, 排序=17}]\",\n" +
//                "\t\"sort\": \"地市名称|在网用户数|日计费收入|月累计费收入|日新发展用户数|月累新发展用户数\"\n" +
//                "}]";
//
//        //System.out.println("list = " + list);
//        System.out.println("emailContentList = " + emailContentList);
//
//        String spliceHtml = spliceHtml(emailHeader, emailTail, emailContentList);
//        System.out.println("spliceHtml = " + spliceHtml);
//
//
////        Map<String, Object> map = new HashMap<>();
////        map.put("emailHeader", "123");
////        map.put("emailContent", "${123}");
////        map.put("emailTail", "123");
////        new MailContent2Html().content2Html(map);
//
//
//    }
}
